<template>
  <div class="login-container">
    <!-- 左侧盒子 -->
    <div class="left-box">
      <!-- 标题区域 -->
      <div class="title-box">
        <img src="../../assets/logo.png" alt="" />
        <span class="title">黑马面面</span>
        <span class="line"></span>
        <span class="sub-title">用户登录</span>
      </div>
      <!-- 表单组件 -->
      <el-form ref="form" :model="form" :rules="rules">
        <!-- 手机号 -->
        <el-form-item prop="phone">
          <el-input
            prefix-icon="el-icon-user"
            v-model="form.phone"
            placeholder="请输入手机号"
          ></el-input>
        </el-form-item>
        <!-- 密码 -->
        <el-form-item prop="password">
          <el-input
            prefix-icon="el-icon-lock"
            v-model="form.password"
            placeholder="请输入密码"
            show-password
          ></el-input>
        </el-form-item>
        <!-- 验证码 -->
        <el-form-item prop="code">
          <el-row>
            <el-col :span="17">
              <el-input
                prefix-icon="el-icon-key"
                v-model="form.code"
                placeholder="请输入验证码"
              ></el-input
            ></el-col>
            <el-col :span="7">
              <img :src="codeURL" @click="changeImg" class="code-img" alt="" />
            </el-col>
          </el-row>
        </el-form-item>
        <!-- 是否勾选 -->
        <el-form-item prop="checked">
          <el-checkbox v-model="form.checked">
            <el-link :underline="false" type="info">我已阅读并同意</el-link>
            <el-link type="primary">用户协议</el-link>
            <el-link :underline="false" type="info">和</el-link>
            <el-link type="primary">隐私条款</el-link>
          </el-checkbox>
        </el-form-item>
        <el-form-item>
          <!-- <el-button class="my-btn" type="primary" @click="submitForm('form')"> -->
          <!-- 不传参的写法 -->
          <el-button class="my-btn" type="primary" @click="submitForm">
            登录</el-button
          >
          <el-button type="primary" @click="showRegister">注册</el-button>
        </el-form-item>
      </el-form>
    </div>
    <!-- 右侧图片 -->
    <img src="../../assets/login_banner_ele (1).png" alt="" />
    <!-- 注册弹框 -->
    <register ref="register"></register>
  </div>
</template>

<script>
// 导入注册组件
import register from './register'
// 导入登录的接口
import { login } from '@/api/login'
export default {
  // 设置name便于调试
  name: 'login',
  // 注册组件
  components: {
    register
  },
  data () {
    return {
      // 表单绑定的数据
      form: {
        // 手机号
        phone: '',
        // 密码
        password: '',
        // 验证码
        code: '',
        checked: []
      },
      // 验证码地址
      codeURL: process.env.VUE_APP_BaseURL + '/captcha?type=login',
      // 校验规则
      rules: {
        phone: [
          {
            required: true,
            message: '手机号不能为空',
            trigger: 'blur'
          },
          {
            pattern: /^(0|86|17951)?(13[0-9]|15[012356789]|166|17[3678]|18[0-9]|14[57])[0-9]{8}$/,
            message: '手机号格式有误',
            trigger: 'blur'
          }
        ],
        password: [
          {
            required: true,
            message: '密码不能为空',
            trigger: 'blur'
          },
          {
            min: 6,
            max: 12,
            message: '密码长度为 6 到 12',
            trigger: 'blur'
          }
        ],
        code: [
          {
            required: true,
            message: '验证码不能为空',
            trigger: 'blur'
          },
          {
            min: 4,
            max: 4,
            message: '验证码长度为4',
            trigger: 'blur'
          }
        ],
        checked: [
          {
            // 必须有内容,而不是布尔值
            required: true,
            message: '必须同意用户协议',
            trigger: 'blur'
          }
        ]
      }
    }
  },
  methods: {
    // 点击切换验证码
    changeImg () {
      this.codeURL =
        process.env.VUE_APP_BaseURL + '/captcha?type=login&t=' + Date.now()
    },
    // 弹出注册框
    showRegister () {
      this.$refs.register.dialogFormVisible = true
    },
    // 登录按钮点击事件
    submitForm () {
      // this.$refs[formName].validate(valid => {
      // 如果不传参 ,直接点出form即可
      this.$refs.form.validate(valid => {
        if (valid) {
          // alert('submit!')
          // this.$message.success('登录成功')
          const { phone, code, password } = this.form
          login({ phone, code, password }).then(res => {
            // console.log('res:', res)
            if (res.data.code === 200) {
              this.$message.success('登录成功!^_^')
            } else {
              this.$message.error(res.data.message)
            }
          })
        } else {
          // console.log('error submit!!')
          this.$message.error('数据格式有误,请检查,滑稽!')
          return false
        }
      })
    }
  }
}
</script>

<style lang="less">
.login-container {
  // 屏幕高度分为100份,1份就是1vh
  height: 100vh;
  // background-color: hotpink;
  background: linear-gradient(#01c6fa 28%, #1493fa 96%);
  display: flex;
  align-items: center;
  justify-content: space-around;
  .left-box {
    width: 478px;
    height: 550px;
    opacity: 1;
    background: #f5f5f5;
    box-sizing: border-box;
    padding-top: 44px;
    padding-left: 42px;
    padding-right: 42px;
    .title-box {
      display: flex;
      align-items: center;
      margin-bottom: 28px; // padding-left: 48px;
      img {
        margin-right: 12px;
        margin-top: 3px;
      }
      .title {
        font-size: 24px;
        margin-right: 12px;
      }
      .line {
        height: 30px;
        background-color: #dedede;
        width: 1px;
        margin-right: 12px;
      }
      .sub-title {
        font-size: 22px;
      }
    }
    .el-button {
      width: 100%;
    }
    .el-button + .el-button {
      margin-left: 0;
    }
    .my-btn {
      margin-bottom: 20px;
    }
  }
  .code-img {
    display: block;
    width: 100%;
    height: 40px;
    cursor: pointer;
  }
}
</style>
